package com.orangeforms.webadmin.app.controller;

import com.alibaba.druid.sql.visitor.functions.Char;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.page.PageMethod;
import com.orangeforms.common.core.annotation.MyRequestBody;
import com.orangeforms.common.core.annotation.NoAuthInterface;
import com.orangeforms.common.core.constant.ErrorCodeEnum;
import com.orangeforms.common.core.object.*;
import com.orangeforms.common.core.util.MyCommonUtil;
import com.orangeforms.common.core.util.MyModelUtil;
import com.orangeforms.common.core.util.MyPageUtil;
import com.orangeforms.webadmin.app.dto.MdmDataOrganizeDto;
import com.orangeforms.webadmin.app.model.MdmDataOrganize;
import com.orangeforms.webadmin.app.service.MdmDataOrganizeService;
import com.orangeforms.webadmin.app.vo.MdmDataOrganizeVo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 组织机构主数据(MdmDataOrganize)表控制层
 *
 * @author makejava
 * @since 2022-09-05 17:24:39
 */
@Api("组织机构主数据")
@Slf4j
@RestController
@RequestMapping("/admin/app/mdmDataOrganize")
public class MdmDataOrganizeController {
    /**
     * 服务对象 
     *  如果接口方法标记 NoAuthInterface 注解，可以直接跳过Token鉴权验证，这里主要为了测试接口方便
     */
    @Autowired
    private MdmDataOrganizeService mdmDataOrganizeService;

    /**
     * 新增数据
     * 
     * @param mdmDataOrganizeDto 新增对象。
     * @return  应答结果对象，包含新增对象主键Id。
     */
    @ApiOperation(value = "新增数据-add")
    @PostMapping("/add")
     public ResponseResult<String> add(@MyRequestBody MdmDataOrganizeDto mdmDataOrganizeDto){
     
        String errorMessage = MyCommonUtil.getModelValidationError(mdmDataOrganizeDto, false);
        if (errorMessage != null) {
            return ResponseResult.error(ErrorCodeEnum.DATA_VALIDATED_FAILED, errorMessage);
        }
        MdmDataOrganize mdmDataOrganize = MyModelUtil.copyTo(mdmDataOrganizeDto, MdmDataOrganize.class);
        // 验证关联Id的数据合法性
        CallResult callResult = mdmDataOrganizeService.verifyRelatedData(mdmDataOrganize, null);
        if (!callResult.isSuccess()) {
            return ResponseResult.errorFrom(callResult);
        }
        mdmDataOrganize = mdmDataOrganizeService.saveNew(mdmDataOrganize);
        return ResponseResult.success(mdmDataOrganize.getOrgCode());
     }
     
     /**
     * 更新数据。
     *
     * @param mdmDataOrganizeDto 更新对象。
     * @return 应答结果对象。
     */
    @ApiOperation(value = "更新数据-update")
    @PostMapping("/update")
    public ResponseResult<Void> update(@MyRequestBody MdmDataOrganizeDto mdmDataOrganizeDto) {
        String errorMessage = MyCommonUtil.getModelValidationError(mdmDataOrganizeDto, true);
        if (errorMessage != null) {
            return ResponseResult.error(ErrorCodeEnum.DATA_VALIDATED_FAILED, errorMessage);
        }
        MdmDataOrganize mdmDataOrganize = MyModelUtil.copyTo(mdmDataOrganizeDto, MdmDataOrganize.class);
        MdmDataOrganize originalMdmDataOrganize = mdmDataOrganizeService.getById(mdmDataOrganize.getOrgCode());
        if (originalMdmDataOrganize == null) {
            // NOTE: 修改下面方括号中的话述
            errorMessage = "数据验证失败，当前 [数据] 并不存在，请刷新后重试！";
            return ResponseResult.error(ErrorCodeEnum.DATA_NOT_EXIST, errorMessage);
        }
        // 验证关联Id的数据合法性
        CallResult callResult = mdmDataOrganizeService.verifyRelatedData(mdmDataOrganize, originalMdmDataOrganize);
        if (!callResult.isSuccess()) {
            return ResponseResult.errorFrom(callResult);
        }
        if (!mdmDataOrganizeService.update(mdmDataOrganize, originalMdmDataOrganize)) {
            return ResponseResult.error(ErrorCodeEnum.DATA_NOT_EXIST);
        }
        return ResponseResult.success();
    }
    
        /**
     * 删除合同段数据。
     *
     * @param id 删除对象主键Id。
     * @return 应答结果对象。
     */
    @ApiOperation(value = "删除数据-delete")
    @PostMapping("/delete")
    public ResponseResult<Void> delete(@MyRequestBody Long id) {
        String errorMessage;
        if (MyCommonUtil.existBlankArgument(id)) {
            return ResponseResult.error(ErrorCodeEnum.ARGUMENT_NULL_EXIST);
        }
        return this.doDelete(id);
    }
    
     /**
     * 列出符合过滤条件的数据列表。
     *
     * @param mdmDataOrganizeDtoFilter 过滤对象。
     * @param orderParam 排序参数。
     * @param pageParam 分页参数。
     * @return 应答结果对象，包含查询结果集。
     */
    @PostMapping("/list")
    @ApiOperation(value = "列出数据-delete")
    public ResponseResult<MyPageData<MdmDataOrganizeVo>> list(
            @MyRequestBody MdmDataOrganizeDto mdmDataOrganizeDtoFilter,
            @MyRequestBody MyOrderParam orderParam,
            @MyRequestBody MyPageParam pageParam) {

        log.info("调用list");
        if (pageParam != null) {
            PageMethod.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        }
        MdmDataOrganize mdmDataOrganizeFilter = MyModelUtil.copyTo(mdmDataOrganizeDtoFilter, MdmDataOrganize.class);
        String orderBy = MyOrderParam.buildOrderBy(orderParam, MdmDataOrganize.class);
        List<MdmDataOrganize> mdmDataOrganizeList =
                mdmDataOrganizeService.getMdmDataOrganizeListWithRelation(mdmDataOrganizeFilter, orderBy);
        return ResponseResult.success(MyPageUtil.makeResponseData(mdmDataOrganizeList, MdmDataOrganize.INSTANCE));
    }

    @PostMapping("/test")
    @ApiOperation(value = "测试功能-test")
    @NoAuthInterface
    public ResponseResult<Void> test(){
        List<MdmDataOrganize> mdmDataOrganizeList = mdmDataOrganizeService.list();
        List<String> mdmOrgNameList = new ArrayList<>();
        for(MdmDataOrganize mdmDataOrganize:mdmDataOrganizeList){
            mdmOrgNameList.add(mdmDataOrganize.getOrgName());
        }

        for(String mdmOrgName:mdmOrgNameList){
            if(mdmOrgName.contains("建设办")){
                char charOne = mdmOrgName.charAt(0);
                char charTwo = mdmOrgName.charAt(1);
                StringBuilder stringBuilder = new StringBuilder("where name like '%");
                stringBuilder.append(charOne).append("%").append(charTwo).append("%建设%'");
                log.info("打印模糊查询条件:{}",stringBuilder.toString());

                mdmDataOrganizeService.getMdmDataOrganizeListLike(mdmOrgName,stringBuilder.toString());

//                mdmDataOrganizeService.getMdmDataOrganizeList()
            }


        }

        return ResponseResult.success();
    }

    /**
     * 查看指定对象详情。
     *
     * @param id 指定对象主键Id。
     * @return 应答结果对象，包含对象详情。
     */
    @GetMapping("/view")
    @ApiOperation(value = "查看数据-delete")
    public ResponseResult<MdmDataOrganizeVo> view(@RequestParam Long id) {
        if (MyCommonUtil.existBlankArgument(id)) {
            return ResponseResult.error(ErrorCodeEnum.ARGUMENT_NULL_EXIST);
        }
        MdmDataOrganize mdmDataOrganize = mdmDataOrganizeService.getByIdWithRelation(id, MyRelationParam.full());
        if (mdmDataOrganize == null) {
            return ResponseResult.error(ErrorCodeEnum.DATA_NOT_EXIST);
        }
        MdmDataOrganizeVo mdmDataOrganizeVo = MdmDataOrganize.INSTANCE.fromModel(mdmDataOrganize);
        return ResponseResult.success(mdmDataOrganizeVo);
    }
    
     private ResponseResult<Void> doDelete(Long id) {
        String errorMessage;
        // 验证关联Id的数据合法性
        MdmDataOrganize originalMdmDataOrganize = mdmDataOrganizeService.getById(id);
        if (originalMdmDataOrganize == null) {
            // NOTE: 修改下面方括号中的话述
            errorMessage = "数据验证失败，当前 [对象] 并不存在，请刷新后重试！";
            return ResponseResult.error(ErrorCodeEnum.DATA_NOT_EXIST, errorMessage);
        }
        if (!mdmDataOrganizeService.remove(id)) {
            errorMessage = "数据操作失败，删除的对象不存在，请刷新后重试！";
            return ResponseResult.error(ErrorCodeEnum.DATA_NOT_EXIST, errorMessage);
        }
        return ResponseResult.success();
    }

}

